﻿using FireChat.Client.Session.Actions;
using FireChat.Share.Web.Actions;
using FireChat.Share.Web.Package;
using FireChat.Share.Web.Util;

namespace FireChat.Client.Session.Receiver;


/// <summary>
/// 远程客户端扩展方法
/// </summary>
internal static class LocalClientReceiverScaffold
{
    /// <summary>
    /// 构建该类型的数据包
    /// </summary>
    /// <param name="id">数据包Id</param>
    /// <param name="data">附带数据</param>
    public static ClientPackage CreatePackage(this ServertActions type, object? data = null)
    {
        return new ClientPackage()
        {
            Id = Guid.NewGuid().ToString(),
            Data = data,
            ActionType = type
        };
    }

    /// <summary>
    /// 执行动作
    /// </summary>
    public static void ExcuteAction(this PackageReceiver receiver)
    {
        var action = ActionScaffold<IClientAction>.Actions.FirstOrDefault(x => x.Type == receiver.Package.ActionType);
        action?.Excute(receiver);
    }
}
